package cn.edu.sjtu.se.dslab.component.design.generate;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.edu.sjtu.se.dslab.component.design.Button;
import cn.edu.sjtu.se.dslab.component.dp.parse.DesignPatternInstanceBean;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.service.IButtonService;

@Service("keyCodeGenerator")
public class KeyCodeGenerator extends DesignPatternCodeGenerator {

	private static final String CONTROL_KEY_CODES_TEMPLATE_NAME = "control_key_codes.vm";
	//private static final String CONTROL_KEY_CODES_FILE_NAME = "CDSF_Injection_Control.h";
	private static final String CONTROL_KEY_CODES_FILE_NAME = "CDSF_Configs_Control.h";

	@Resource(name = "buttonService")
	IButtonService buttonService;

	public KeyCodeGenerator() {
		super();
	}

	@Override
	public void generateCode(String targetDirPath, Project project, DesignPatternInstanceBean ...dpiBean) throws IOException {
		List<Button> buttonList = buttonService.getButtonByProject(project.getId());
		Collections.sort(buttonList);
		inputData("keyCodes", buttonList);
		StringBuilder keyCodesFile = new StringBuilder(targetDirPath);
		keyCodesFile.append("/CASFConfigs/").append(CONTROL_KEY_CODES_FILE_NAME);
		generateCodeFile(keyCodesFile.toString(), CONTROL_KEY_CODES_TEMPLATE_NAME);
	}

}
